package com.mybatistest.controller;

import com.mybatistest.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
public class UploadController {

    @PostMapping("/upload")
    public Result upload(String usernamej, MultipartFile image) throws IOException {
        log.info("上传文件：{}", image);
        String originalFilename = image.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");
        String extname = originalFilename.substring(i);
        String newFileName = UUID.randomUUID().toString() + extname;
        log.info("上传文件名：{}", newFileName);
        image.transferTo(new File("D:\\work\\java\\SpringBootCode\\mybatistest\\src\\main\\resources\\static\\" + newFileName));

        return Result.success("上传成功");
    }
}
